const Router = require('@koa/router')
const jwt = require("jsonwebtoken")
const { CatModel } = require('../databases/index')
const router = new Router()
router.get("/getAllcat", async(ctx) => {
    const cat = await CatModel.findAll();
    ctx.body = {
        code: 200,
        msg: "success",
        data: {
            cat
        }
    }
})
router.get("/gettwocat", async(ctx) => {
    const cat = await CatModel.findAll({
        limit:2
    });
    if(cat){
        ctx.body = {
        code: 200,
        msg: "success",
        data: {
            cat
        }
    }
    }
    
})
router.post("/delcat", async(ctx) => {
    const { selectcat } = ctx.request.body;
    console.log(selectcat);
    // const order = await OrderModel.findAll();
    const select = await CatModel.destroy({ where: { catID: selectcat} });
    console.log('详情', select)
    if (select) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",
                
            }

        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "失败"
            }
        }
    }
})
router.post("/getOnecat", async(ctx) => {
    const { catID } = ctx.request.body;
    // console.log(catID);
    // const order = await OrderModel.findAll();
    const onecat = await CatModel.findOne({ where: { catID: catID } });
    console.log('订单详情', onecat)
    if (onecat) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "updateOrder success",
                onecat
            }

        }
    } else {
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "订单不存在"
            }
        }
    }
})
router.post("/updatecat", async(ctx) => {
    const  {addForm}  = ctx.request.body;
    console.log(addForm,11111111111111111);
    const onecat = await CatModel.update(
        {
            title:addForm.title,
            img:addForm.img,
            gengder:addForm.gengder,
            state:addForm.state,
            address:addForm.address,
            content:addForm.content,
            telephone:addForm.telephone
            }, 
        {
        where: {
            catID: addForm.ID
        }
    });
    if (onecat) {
        ctx.body = {
            code: 200,
            msg: "success",
        }
    } else {
        ctx.body = {
            code: 500,
            msg: "fail",
        }
    }
})
router.get("/getcat", async(ctx) => {
    const { currentPage,pageSize }=ctx.request.query;
    // console.log( currentPage,pageSize)
    offset=(currentPage-1)*pageSize||0;
    const result= await CatModel.findAndCountAll(
        { 
            offset,
            limit:parseInt(pageSize),
        }
    );
    if(result){
        ctx.body ={
            code: 200,
            msg: "success",
            data: {
                result
            }
    }}
    
})
router.post("/deleteOnecat", async(ctx) => {
    const { catID } = ctx.request.body;
    console.log(catID,11111111111111111);
    const dl = await CatModel.destroy({ where: { catID:catID } })
    if (dl) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",
            }
        }
    }else{
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "错误",
            }
        }
    }

    
})
router.post("/addcat", async(ctx) => {
    const { addForm } = ctx.request.body;
    console.log(addForm);
    const dl = await CatModel.create({
        title:addForm.title,img:addForm.img,gengder:addForm.gengder,state:addForm.state,
        address:addForm.address,content:addForm.content,telephone:addForm.telephone
            
    })
    if (dl) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",
            }
        }
    }else{
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "错误",
            }
        }
    }
})
router.post("/search", async(ctx) => {
    const { Op } = require("sequelize");
    const { value } = ctx.request.body;
    const dl = await CatModel.findAll(
        {
            where: {
                [Op.or]: [
                    {
                    title: {
                        [Op.like]: '%'+value+'%'
                    }
                    },
                    {
                        catID: {
                        [Op.like]: '%'+value+'%'
                    }
                    }
                ]
                }
          }
    )
    if (dl) {
        ctx.body = {
            code: 200,
            msg: "success",
            data: {
                message: "success",
                dl
            }
        }
    }else{
        ctx.body = {
            code: 200,
            msg: "error",
            data: {
                message: "错误",
            }
        }
    }
})

module.exports = router